Dart List operator ==
Syntax & Examples
Syntax of List.operator ==
The syntax of List.operator == operator is:
operator ==(Object other) → boolThis operator == operator of List whether this list is equal to other.
Parameters
| Parameter | Optional/Required | Description |
|---|---|---|
other | required | the object to compare with this list |
✐ Examples
1 Check equality of two integer lists
In this example,
- We create three lists,
list1,list2, andlist3, each containing integers. - We then compare
list1withlist2using the==operator, which returnstrueif the lists have the same elements in the same order. - We also compare
list1withlist3, which returnsfalseas the elements are different. - We print the results to standard output.
Dart Program
void main() {
List<int> list1 = [1, 2, 3];
List<int> list2 = [1, 2, 3];
List<int> list3 = [4, 5, 6];
print(list1 == list2); // Output: true
print(list1 == list3); // Output: false
}Output
true false
2 Check equality of two string lists
In this example,
- We create three lists,
list1,list2, andlist3, each containing strings. - We then compare
list1withlist2using the==operator, which returnstrueif the lists have the same elements in the same order. - We also compare
list1withlist3, which returnsfalseas the elements are different. - We print the results to standard output.
Dart Program
void main() {
List<String> list1 = ['a', 'b', 'c'];
List<String> list2 = ['a', 'b', 'c'];
List<String> list3 = ['x', 'y', 'z'];
print(list1 == list2); // Output: true
print(list1 == list3); // Output: false
}Output
true false
3 Check equality of two dynamic lists
In this example,
- We create three lists,
list1,list2, andlist3, each containing elements of various types. - We then compare
list1withlist2using the==operator, which returnstrueif the lists have the same elements in the same order. - We also compare
list1withlist3, which returnsfalseas the elements are different. - We print the results to standard output.
Dart Program
void main() {
List<dynamic> list1 = [1, 'two', 3.0];
List<dynamic> list2 = [1, 'two', 3.0];
List<dynamic> list3 = [4, 'five', 6.0];
print(list1 == list2); // Output: true
print(list1 == list3); // Output: false
}Output
true false
Summary
In this Dart tutorial, we learned about operator == operator of List: the syntax and few working examples with output and detailed explanation for each example.